// useCounter.ts
import { ref, computed, type ComputedRef, type Ref } from 'vue';

interface UseCounterOptions {
  min?: number;
  max?: number;
}

interface UseCounterReturn {
  count: Ref<number>;
  increment: () => void;
  decrement: () => void;
  reset: () => void;
  isMin: ComputedRef<boolean>;
  isMax: ComputedRef<boolean>;
}

export function useCounter(
  initialValue: number = 0,
  options: UseCounterOptions = {},
): UseCounterReturn {
  const { min = -Infinity, max = Infinity } = options;
  const count = ref(Math.max(min, Math.min(max, initialValue)));

  const increment = () => {
    if (count.value < max) {
      count.value++;
    }
  };

  const decrement = () => {
    if (count.value > min) {
      count.value--;
    }
  };

  const reset = () => {
    count.value = initialValue;
  };

  const isMin = computed(() => count.value <= min);
  const isMax = computed(() => count.value >= max);

  return {
    count,
    increment,
    decrement,
    reset,
    isMin,
    isMax,
  };
}
